package org.earthcube.iris.wps;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;

import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletContext;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.deegree.commons.xml.XMLAdapter;
import org.deegree.services.wps.output.ComplexOutput;
import org.deegree.services.wps.provider.JavaProcessProviderProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * these tiles are located in src/main/resources/org/earthcube/iris/wps
 */
public class FormatCreatorTest {
	private static final Logger LOG = LoggerFactory
			.getLogger(FormatCreatorTest.class);
	private static XMLInputFactory factory = XMLInputFactory.newInstance();
	
	public void readAscii(ComplexOutput os) {
		String filename = "ascii.txt";
		LOG.trace("BEGIN FormatCreatorTest#readAscii()");
		try {
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();

			XMLStreamWriter writer = os.getXMLStreamWriter();
			 XMLAdapter.writeElement( writer, "string", is.toString() );
			
			//writer.writeCharacters(is.toString());
			writer.flush();
		} catch (XMLStreamException e) {
			LOG.error(e.getMessage());
		} catch (IOException e) {
			LOG.error(e.getMessage());
		}
	}
	
	public void readAscii2(ComplexOutput os) {
		String filename = "ascii.txt";
		LOG.trace("BEGIN FormatCreatorTest#readAscii()");
		try {
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			byte[] buffer = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(buffer, 0, bytesRead);
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}
	}
	
	public void readAudio(ComplexOutput os) {
		String filename = "audio.wav";

		try {
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			byte[] buffer = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(buffer, 0, bytesRead);
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}
	}
	public void readPlot(ComplexOutput os) {
		String filename = "plot.png";

		try {
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			byte[] buffer = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(buffer, 0, bytesRead);
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}
	}
	
	public void readSaca(ComplexOutput os) {
		String filename = "saca.SAC";

		try {
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			byte[] buffer = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(buffer, 0, bytesRead);
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}
	}
	
	public void readSacbb(ComplexOutput os) {
		String filename = "sacbb.SAC";

		try {
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			byte[] buffer = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(buffer, 0, bytesRead);
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}
	}
	
	public void readStationXml(ComplexOutput os) {
		String filename = "stationml.xml";

		try {
			
			InputStream is = FormatCreatorTest.class.getResource(filename)
					.openStream();
			factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);

		            XMLStreamWriter writer = os.getXMLStreamWriter();
		            XMLStreamReader reader= factory.createXMLStreamReader (is);
		          if (!  reader.isStartElement()) {reader.next();}
		            XMLAdapter.writeElement( writer, reader );
		        } catch ( XMLStreamException e ) {
		            LOG.error( e.getMessage() );
		        } catch ( IOException e ) {
		            LOG.error( e.getMessage() );
		        }
	}
	
	public void readBiaryTest(ComplexOutput os) {
		String binary = "iVBORw0KGgoAAAANSUhEUgAAAMgAAAAwCAMAAABjR8XYAAADAFBMVEUAAQAAAgABBAACBQEEBwIF";

		try {
			StringReader is = new StringReader(binary);

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			char[] buffer = new char[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(new String(buffer, 0, bytesRead).getBytes("UTF-8"));
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}

	}

	public void readFileTest(ComplexOutput os) {
		String filename = "audio.wav";
		URL res = FormatCreatorTest.class.getResource(filename);
		String binary = null;

		if (res == null)
			binary = "isnullss";
		else
			binary = res.toExternalForm();

		try {
			StringReader is = new StringReader(binary);

			BufferedOutputStream bs = new BufferedOutputStream(
					os.getBinaryOutputStream());

			char[] buffer = new char[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(buffer)) != -1) {
				bs.write(new String(buffer, 0, bytesRead).getBytes("UTF-8"));
			}
			bs.flush();
		} catch (IOException e1) {
			LOG.error(e1.getMessage());
		}

	}
}
